<?php
	$TEMP->set_file(array('handle' => DIR_VIEW_DEAN . 'newSubject.html'));
	$SLID 	= _GET('slid');
	$SID 	= _GET('sid');
	
	$semesterLabel = $DSC->getSemesterLabel($SLID);
		$CID 	   = $semesterLabel['course_idFK'];
		$semDesc   = $semesterLabel['sem_desc'];
		$semCode   = $semesterLabel['sem_code'];
	
	if ( isset($_POST['subButton']) ) {
		
		$if_error		= 0;
		
		$txtdesc			 = _POST('txtdesc');
		$txtcode			 = _POST('txtcode');
		$txtlec				 = _POST('txtlec');
		$txtlab				 = _POST('txtlab');
		$txtleclab			 = _POST('txtleclab');
		$txttotal			 = _POST('txttotal');
		$txttotalparenthesis = _POST('txttotalparenthesis');
		$txtparenthesis 	 = _POST('txtparenthesis');
		$txtco				 = _POST('txtco');
		$txtpre				 = _POST('txtpre');
		$txthrs				 = _POST('txthrs');
		$txtsaturday		 = _POST('txtsaturday');
		
		$lec		= (!empty($txtlec) 		? $txtlec 	 : 0);
		$lab		= (!empty($txtlab) 		? $txtlab 	 : 0);
		$leclab		= (!empty($txtleclab) 	? $txtleclab : 0);
		$total		= ($txtparenthesis == 'on') ? -abs($txttotal) : $txttotal;
		$co			= $DSC->getRequisiteSubject($txtco, $CID);
		$pre		= $DSC->getRequisiteSubject($txtpre, $CID);
		$hrs		= (!empty($txthrs) 		? $txthrs 	 : 0);

		if ( empty($txtdesc) )	{ $if_error++; $_txtdesc 	= REQ_FIELD; }
		if ( empty($txtcode) )	{ $if_error++; $_txtcode 	= REQ_FIELD; }
		
		if ( $if_error == 0 ) {
			
			$strArray = array('subj_desc'		=> $txtdesc,
							  'subj_code' 	 	=> upperCase($txtcode),
							  'total_units'	 	=> $total,
							  'lec_units'		=> $lec,
							  'lab_units'	 	=> $lab,
							  'lec_lab_units'	=> $leclab,
							  'is_saturday'		=> $txtsaturday,
							  'co_req'	 		=> $co,
							  'pre_req'	 		=> $pre,
							  'total_hrs'	 	=> $hrs,
							  'sem_linker_id'	=> $SLID,
							  'year_id'			=> $activeYear,
							  'course_id'		=> $CID);

			if (empty($SID)) {
				$subjLastID = $DSC->insertSubject($strArray);
			}
			else {
				$subjLastID = $DSC->updateSubject($strArray, $SID);
			}
			redirect('?page=Subject&cid=' . $semesterLabel['course_idFK'] .'&s=' . $subjLastID);
		}
		
	}
	else {
		if ( !empty($SID) ) {
			$getSubject = $DSC->getSubject($SID);

			if(!empty($getSubject)) {
				$txtdesc			 = $getSubject['subj_desc'];
				$txtcode			 = $getSubject['subj_code'];
				$txtlec				 = $getSubject['lec_units'];
				$txtlab				 = $getSubject['lab_units'];
				$txtsaturday		 = $getSubject['is_saturday'];
				$txtleclab			 = $getSubject['lec_lab_units'];
				$txttotal			 = abs($getSubject['total_units']);
				$total			 	 = $getSubject['total_units'];
				$txtco				 = $DSC->getReqSubjectID($getSubject['co_req'], false);
				$txtpre				 = $DSC->getReqSubjectID($getSubject['pre_req'], false);
				$txthrs				 = $getSubject['total_hrs'];
			}
		}
		else {
			$txtdesc			 = '';
			$txtcode			 = '';
			$txtlec				 = 0;
			$txtlab				 = 0;
			$txtleclab			 = 0;
			$txttotal			 = 0;
			$txtco				 = '';
			$txtpre				 = '';
			$txthrs				 = 0;
			$txtchecked			 = '';
			$saturday			 = '';
		}
	}
				
	$saturday 	 		= ($txtsaturday == 'Y') ? 'checked="checked"' : '';

	$txtchecked		 	 = ($total < 0) ? 'checked="checked"' : '';
	
	
	$TEMP->set_var(array('dean_sub_menu'	=> $pageLib->deanSubMenu(DIR_IMAGES),
						 'semester_label' 	=> $semDesc . ' (' . $semCode . ')',
						 'w_txtdesc' 		=> $_txtdesc,
						 'w_txtcode' 		=> $_txtcode,
						 'txtdesc' 			=> $txtdesc,
						 'txtcode' 			=> $txtcode,
						 
						 'txtlec' 			=> $txtlec,
						 'txtlab' 			=> $txtlab,
						 'txtleclab' 		=> $txtleclab,
						 'txttotal' 		=> $txttotal,
						 'txtco' 			=> $txtco,
						 'txtpre' 			=> $txtpre,
						 'txthrs' 			=> $txthrs,
						 'txthrs' 			=> $txthrs,
						 
						 'txtchecked'		=> $txtchecked,
						 'saturday'			=> $saturday,
						 
						 'dept_course'		=> $strAccCourse,
						 'course_id'		=> $CID,
						 'sem_linker_id'	=> $SLID
						));
?>